package com.espirit.hr;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.espirit.eap.sa.Employee;
import com.espirit.finance.core.EntityAccountAware;
import com.espirit.finance.core.Voucher;
import com.espirit.finance.core.VoucherService;
import com.espirit.finance.core.VoucherServiceAware;
import com.googlecode.cswish.struts.spring.GenericService;

@Service
public class EmployeeAccountService implements VoucherServiceAware{
	@Resource
	VoucherService voucherService;
	
	@Resource
	GenericService genericService;
	
	@Resource
	EmployeeService employeeService;
	
	@Transactional
	public  void add(EntityAccountAware entityAccountAware){
		voucherService.add(entityAccountAware);
	}
	
	@Override
	public void onAdd(Voucher voucher) {
		EmployeeSalary employeeVoucher = (EmployeeSalary) voucher;
		EmployeeAccount employeeAccount = (EmployeeAccount) voucher.getEntityAccountAware();
		employeeVoucher.setEmployee(employeeAccount.getEmployee());
	}

	@Override
	public Class<EmployeeSalary> getVoucherClazz() {
		return EmployeeSalary.class;
	}

	@Override
	public List<SalaryAcctItem> getAccountBookStandards(
			EntityAccountAware entityAccountAware) {
		EmployeeAccount employeeAccount = (EmployeeAccount) entityAccountAware;
		if (employeeAccount.getEmployee() == null) return Collections.EMPTY_LIST;
		Integer empId = employeeAccount.getEmployee().getId();
		return employeeService.getSalaryAccountBookStandards(empId);
	}
	
	
	
	public EmployeeAccount generateEntityAccount(Employee emp, Integer year, Integer month) {
		EmployeeAccount ea = new EmployeeAccount();
		
		ea.setEmployee(emp);

		Integer deliveryTimes = getDeliveryTimes(emp, year ,month);
		ea.setDeliveryTimes(deliveryTimes);
		
		BigDecimal faultAmounts = getFaultAmounts(emp, year, month);
		ea.setFaultAmount(faultAmounts);
		
		
		List<DutyRecord> dutyRecords = getEmployeeDutyRecords(emp, year, month);
		
		// dutyrecords can fetch.
		ea.setLeaveAppHours(BigDecimal.valueOf(18));
		ea.setOtHours(BigDecimal.valueOf(80));
		
		EmployeeAccountLateItem lateItem = new EmployeeAccountLateItem();
		lateItem.setParent(ea);
		lateItem.setMinutes(6);
		ea.safeLateItems().add(lateItem);
		
		ea = genericService.add(ea);
		return ea;
	}

	//TODO
	private BigDecimal getFaultAmounts(Employee emp, Integer year, Integer month) {
		return null;
	}

	private Integer getDeliveryTimes(Employee emp, Integer year, Integer month) {
		return 0;
	}

	private List<DutyRecord> getEmployeeDutyRecords(Employee emp, Integer year,
			Integer month) {
		return Collections.EMPTY_LIST;
	}

}
